package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.adapter.AddConstructContentAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemCheckedChangeListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 工程 工单管理 派发工单 工程施工 编辑派工信息 添加施工内容
 * Created by tony on 2018/2/1.
 */

public class AddConstructContentForEngineeringDispatchActivity extends Activity implements View.OnClickListener, ItemCheckedChangeListener {
    private ImageView img_back;
    private TextView tv_title;
    private TextView btn_edit;
    private ListView lv;
    private AddConstructContentAdapter adapter;
    private List<EngineeringConstructContentBean> datas = new ArrayList<>();
    private final String TAG = AddConstructContentForEngineeringDispatchActivity.class.getSimpleName();
    private List<String> selectedItemList = new ArrayList<String>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_add_construct_content_for_engineering_dispatch);

        init();
    }

    private void init() {
        img_back = (ImageView) findViewById(R.id.img_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        btn_edit = (TextView)findViewById(R.id.btn_edit);
        lv = (ListView)findViewById(R.id.lv);


        tv_title.setText(getResources().getText(R.string.add_construction_content));
        img_back.setOnClickListener(this);
        btn_edit.setOnClickListener(this);
//
//        datas.add(new EngineeringConstructContentBean("准备施工队伍", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("现场安装基础制作", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("安装基础检验", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("安装点位确认", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("相关手续或许可办理", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("现场接货验收", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("现场产品管理", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("施工设备租赁", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("联系现场安装设备入场", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("现场制作内容的进度", "2017-05-21", "2017-05-27"));
//        datas.add(new EngineeringConstructContentBean("安装进度", "2017-05-21", "2017-05-27"));

//       Intent mIntent = getIntent();
//        if (mIntent != null) {
//            selectedItemList = (List<String>) mIntent.getSerializableExtra(ConstantUtil.KEY_1);
//            for (String posStr : selectedItemList) {
//                int positon = Integer.parseInt(posStr);
//                datas.get(positon).setChecked(true);
//            }
//
//        }

        adapter = new AddConstructContentAdapter(this, TAG);
        adapter.setData(datas);
        adapter.setItemCheckedChangeListener(this);
        lv.setAdapter(adapter);


    }

    private void completionEdit() {
        List<EngineeringConstructContentBean> selectedDatas = new ArrayList<>();
        for (String posStr : selectedItemList) {
            int positon = Integer.parseInt(posStr);
            selectedDatas.add(datas.get(positon));
        }

        Intent mIntent = new Intent();
        mIntent.putExtra(ConstantUtil.KEY_1, (Serializable) selectedItemList);
        mIntent.putExtra(ConstantUtil.KEY_2, (Serializable) selectedDatas);
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
        Toast.makeText(this, "编辑成功", Toast.LENGTH_SHORT).show();

        finish();
    }

    @Override
    public void checkedChangeListener(int position, boolean isSlected) {
        String posStr = position + "";
        if (isSlected) {
            if (!selectedItemList.contains(posStr)) {
                selectedItemList.add(posStr);
            }
        } else {
            if (selectedItemList.contains(posStr)) {
                selectedItemList.remove(posStr);
            }
        }


    }

    @Override
    public void checkedChangeListener(View view, int position, boolean isSlected) {

    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_edit:
                completionEdit();
                break;

        }
    }
}
